package com.techsoul.module.pms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.goods.mapper.SkuMapper;
import com.techsoul.module.goods.pojo.entity.Sku;
import com.techsoul.module.goods.pojo.vo.SkuVo;
import com.techsoul.module.pms.enmus.MaterialBaseOnEnum;
import com.techsoul.module.pms.enmus.WmsInStatusEnum;
import com.techsoul.module.pms.mapper.TbWmsTemplateLineMapper;
import com.techsoul.module.pms.mapper.TbWmsTemplateMapper;
import com.techsoul.module.pms.pojo.entity.TbWmsMaterialReceiptLine;
import com.techsoul.module.pms.pojo.entity.TbWmsTemplate;
import com.techsoul.module.pms.pojo.entity.TbWmsTemplateLine;
import com.techsoul.module.pms.pojo.param.*;
import com.techsoul.module.pms.pojo.vo.TbWmsTemplateVo;
import com.techsoul.module.pms.service.ITbWmsTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 出入库模板 Service实现
 * 
 * @author liuen
 * @date 2022-01-25
 */
@Service
@Transactional(readOnly = true)
public class TbWmsTemplateServiceImpl implements ITbWmsTemplateService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TbWmsTemplateMapper mapper;
    @Autowired
    private TbWmsTemplateLineMapper tbWmsTemplateLineMapper;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SkuMapper skuMapper;
    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(TbWmsTemplateSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();

        TbWmsTemplate entity = new TbWmsTemplate();
        BeanUtils.copyProperties(params, entity);

        entity.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
        entity.setTemplateId(entity.getId());
          this.mapper.insert(entity);
        List<TbWmsTemplateLineSaveParam> tbWmsTemplateLineSaveParams = params.getItemList();
        for(int i=0; i<tbWmsTemplateLineSaveParams.size();i++){
            TbWmsTemplateLine tbWmsTemplateLine = new TbWmsTemplateLine();
            TbWmsTemplateLineSaveParam tbWmsTemplateLineSaveParam = (TbWmsTemplateLineSaveParam)tbWmsTemplateLineSaveParams.get(i);
            BeanUtils.copyProperties(tbWmsTemplateLineSaveParam, tbWmsTemplateLine);
            tbWmsTemplateLine.setLineId(new Long(i));
            tbWmsTemplateLine.setTemplateId(entity.getId());
            SkuVo sku = this.skuMapper.findById(tbWmsTemplateLineSaveParam.getMaterialNo());
            tbWmsTemplateLine.setMaterialName(sku.getSkuName());
            tbWmsTemplateLine.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
            this.tbWmsTemplateLineMapper.insert(tbWmsTemplateLine);
        }
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(TbWmsTemplateSaveParam params) {
        TbWmsTemplate entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(sysUser.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public TbWmsTemplateVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<TbWmsTemplateVo> findAll(TbWmsTemplateSearchParam params) {
        return this.mapper.findAll(params);
    }

    @Transactional(readOnly = false)
    @Override
    public String insertAll(TbWmsTemplateSaveParam params){
        // 收货人编码为空时，新增收货人
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();



        params.setSiteCode(sysUser.getOrgId());



        String receiptId = this.insert(params);
        // 新增运单明细信息
     //  List<TbWmsTemplateLine> tbWmsTemplateLines= insertItem(params, receiptId,sysUser);


        return "success";
    }

    @Transactional(readOnly = false)
    @Override
    public  List<TbWmsTemplateLine> insertItem(TbWmsTemplateSaveParam params, String receiptId,UserVo sysUser) {
        List<TbWmsTemplateLine> tbWmsTemplateLines = new ArrayList<TbWmsTemplateLine>();
        if(ListUtils.isNotEmpty(params.getItemList())){
            int itemNo = 1;

            for (TbWmsTemplateLineSaveParam item : params.getItemList()) {
                TbWmsTemplateLine tbWmsTemplateLine = new TbWmsTemplateLine();
                item.setTemplateId(receiptId)
                        .setCutomerId(params.getCustomerNo())
                        .setCutomerNo(params.getCustomerNo())
                        .setLineId(Long.valueOf(itemNo++));

                BeanUtils.copyProperties(item, tbWmsTemplateLine);
                tbWmsTemplateLine.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
                this.tbWmsTemplateLineMapper.insert(tbWmsTemplateLine);


                tbWmsTemplateLines.add(tbWmsTemplateLine);
            }
        }
        return tbWmsTemplateLines;
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<TbWmsTemplateVo> findAll(TbWmsTemplateSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

}
